package com.xyz.keshe;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.xyz.keshe.dao.AnnouncementDao;
import com.xyz.keshe.model.Announcement;

import java.text.SimpleDateFormat;
import java.util.Locale;

public class AnnouncementDetailActivity extends AppCompatActivity {

    private TextView tvTitle;
    private TextView tvContent;
    private TextView tvTime;
    
    private AnnouncementDao announcementDao;
    private int announcementId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_announcement_detail);

        // 初始化数据库
        announcementDao = new AnnouncementDao(this);

        // 初始化视图
        tvTitle = findViewById(R.id.tv_announcement_title);
        tvContent = findViewById(R.id.tv_announcement_content);
        tvTime = findViewById(R.id.tv_announcement_time);

        // 获取传递的公告ID
        Intent intent = getIntent();
        announcementId = intent.getIntExtra("announcement_id", 0);

        if (announcementId == 0) {
            Toast.makeText(this, "公告不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 加载公告详情
        loadAnnouncementDetail();
    }

    private void loadAnnouncementDetail() {
        Announcement announcement = announcementDao.getAnnouncementById(announcementId);
        
        if (announcement == null) {
            Toast.makeText(this, "公告不存在", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }

        // 显示公告信息
        tvTitle.setText(announcement.getTitle());
        tvContent.setText(announcement.getContent());
        
        // 格式化时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm", Locale.getDefault());
        tvTime.setText("发布时间：" + sdf.format(announcement.getCreateTimeAsDate()));
    }
} 